﻿Imports System.Xml.Serialization
Imports System.Xml

Public Class ListePMT
    Inherits List(Of IPMT)
    Implements IXmlSerializable

    Public Function GetSchema() As Xml.Schema.XmlSchema Implements IXmlSerializable.GetSchema
        Return Nothing
    End Function

    Public Sub ReadXml(reader As Xml.XmlReader) Implements IXmlSerializable.ReadXml

        Dim XmlListePMT As XElement = XElement.Load(reader.ReadSubtree)

        For Each PMT_XElement As XElement In XmlListePMT.Elements

            'Les paramètres généraux
            Dim PMTParamGeneraux As XElement = PMT_XElement.<ParametresGeneraux>.FirstOrDefault
            Dim ParamGeneraux As New PMTParametresGeneraux
            ParamGeneraux.ReadXml(PMTParamGeneraux.CreateReader)

            'La gestion de l'arborescence
            Dim PMTGestionArbo As XElement = PMT_XElement.<GestionArbo>.FirstOrDefault
            Dim GestionArbo As New PMTGestionArbo
            GestionArbo.ReadXml(PMTGestionArbo.CreateReader)

            'Les tables du flux
            Dim PMTTablesFlux As XElement = PMT_XElement.<TablesPMTFlux>.FirstOrDefault
            Dim TablesFlux As New PMTTablesflux
            TablesFlux.ReadXml(PMTTablesFlux.CreateReader)

            'Les tables de paramétrage
            Dim PMTTablesParam As XElement = PMT_XElement.<TablesParam>.FirstOrDefault
            Dim TablesParam As New PMTTablesParam
            TablesParam.ReadXml(PMTTablesParam.CreateReader)

            'Les tables actuarielles
            Dim PMTTablesActuarielles As XElement = PMT_XElement.<TablesActuarielles>.FirstOrDefault
            Dim TablesActuarielles As New PMTTableActuarielle
            TablesActuarielles.ReadXml(PMTTablesActuarielles.CreateReader)

            'Création du PMT
            Me.Add(New PMT(ParamGeneraux, TablesFlux, TablesParam, TablesActuarielles, GestionArbo))
        Next

        'While (reader.Read())

        '    If (reader.NodeType = XmlNodeType.Element) Then

        '        Dim ParamGeneraux As New PMTParametresGeneraux
        '        If (reader.Name = "ParametresGeneraux") Then
        '            ParamGeneraux.ReadXml(reader)
        '            'reader.Read()
        '        End If

        '    End If

        'End While

        '        Me.Constructeur = reader.GetAttribute("Constructeur")
        '        If (reader.Read()) Then
        '            If (reader.NodeType = XmlNodeType.Text) Then Me.Modele = reader.Value
        '        End If
        '        else if (reader.Name == "Cylindree")
        '        {
        '        If (reader.Read()) Then
        '            {
        '                if (reader.NodeType == XmlNodeType.Text)
        '                    this.Cylindree = int.Parse(reader.Value);
        '            }
        '        }
        '        else if (reader.Name == "Proprietaire")
        '        {
        '                If (reader.Read()) Then
        '            {
        '                if (reader.NodeType == XmlNodeType.Element)
        '                {
        '                    XmlSerializer xs = new XmlSerializer(typeof(Person));
        '                    this.Proprietaire = xs.Deserialize(reader) as Person;
        '                }
        '            }
        '        }
        '                    End If
        'End While

    End Sub

    Public Sub WriteXml(writer As Xml.XmlWriter) Implements IXmlSerializable.WriteXml

        For Each PMT In Me
            PMT.WriteXml(writer)
            'writer.WriteAttributeString("Constructeur", Me.Constructeur)
            'Commentaire XML
            'writer.WriteComment("Hello world from IXmlSerializable !")
            'On ouvre l'élément <Modele>, sans le refermer
            'writer.WriteStartElement("DirectoryExists")
            'Ajoute de l'attribut Constructeur="..."
            'writer.WriteAttributeString("Constructeur", Me.Constructeur)
            'On écrit le nom du modèle dans l'élément <Modele>
            'writer.WriteString(Me.Modele)
            'Fermeture de l'élément <Modele>
            'writer.WriteEndElement()
            'Ajout de l'élément Cylindrée avec son contenu
            'writer.WriteElementString("Cylindree", this.Cylindree.ToString())

            'writer.WriteElementString("DirectoryExists", Me.DirectoryExists.ToString)
            'writer.WriteElementString("DirectoryFullName", Me.DirectoryFullName)
            'writer.WriteElementString("Tag", Me.Tag)

            'Dim override As XmlAttributeOverrides = New XmlAttributeOverrides
            'Dim attr As XmlAttributes = New XmlAttributes
            'attr.XmlRoot = New XmlRootAttribute("ParametresGeneraux")
            'override.Add(GetType(PMTParametresGeneraux), attr)
            'Dim xs As New XmlSerializer(GetType(PMTParametresGeneraux), override)
            'xs.Serialize(writer, PMT.ParametresGeneraux)

        Next
    End Sub
End Class
